//Page Object
Page({
  data: {
    index: 0,
    value: 1,
    num:0,
    total_price:0.00
  },
  click: function () {
    console.log(123)
    this.setData({
      index: 4
    })
  },
  //加
  add: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var _goods = this.data.details;
    var _goods_ = _goods.gift_card_goods;
    //得到点击的值
    var _num = parseInt(e.currentTarget.dataset.num)
    console.log(_num)
    _num += 1;
    for (var k in _goods_){
      _goods_[index].goods_num = _num;
      _goods_[index].flag = false;
    }



    console.log(_goods_,'加号');
    _goods._goods_;
    that.setData({
      details: _goods,
    }),that.sumup(),that.totalprice()
  },
  //减
  reduce: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index;
    var _goods = this.data.details;
    var _goods_ = _goods.gift_card_goods;
    //得到点击的值
    var _num = parseInt(e.currentTarget.dataset.num)
    console.log(_num)
    _num -= 1;
    for (var k in _goods_) {
      _goods_[index].goods_num = _num;
      if (_num <= 0){
        _goods_[index].flag = true;
      }else{
        _goods_[index].flag = false;
      }
      
    }
    console.log(_goods_, '减号');
    _goods._goods_;
    that.setData({
      details: _goods,
    }),that.sumup(),that.totalprice()
  },

  //计算总数Total
  sumup:function(){
    var tath = this;
    var _goods = this.data.details;
    var _goods_ = _goods.gift_card_goods;
    var _num = 0;
    for (var k in _goods_) {       
      _num += _goods_[k].goods_num;

    }

    tath.setData({
      num:_num
    });

  },

  //计算总价
  totalprice:function(){
    var tath = this;
    var _goods = this.data.details;
    var _goods_ = _goods.gift_card_goods;
    var _totalprice = 0;
    for (var k in _goods_) {
      _totalprice += _goods_[k].goods_num * _goods_[k].goods_price;
    }
    var yuan_totalprice = _totalprice.toFixed(2)
    if (tath.data.nowRebate == "0.00") {
      _totalprice = _totalprice;//购买卡券的总价
      console.log(_totalprice + "==============没有折扣的价格")
    } else {
      _totalprice = tath.data.nowRebate * _totalprice
      console.log(_totalprice + "==============zhe 折扣后的价格")
    }

    let zhehou = (_totalprice.toFixed(2) * tath.data.nowRebate).toFixed(2)
    tath.setData({
      total_price: _totalprice.toFixed(2),
      yuan_totalprice: yuan_totalprice
    });
  },

  //购买礼品卡
  buy:function(){
    var tath = this.data;
    var card_name = tath.details.name;//卡券名称
    var card_id = tath.details.id;//卡券id
    
    var total_price = tath.total_price;//购买卡券的总价
    var nowRebate = tath.nowRebate;//本张卡的折扣值

    


    var goods = tath.details.gift_card_goods;
    //获取已选中商品信息
    var _goods = {};
    for(var j in goods){
      if (goods[j].goods_num > 0){
        var arr = goods[j];
        //var str = arr.join(',');
        //_goods.push(arr);
        _goods[j] = arr;
      }
    }
    //var __goods = JSON.stringify(_goods);
    //获取支付参数
    getApp().request({
      url: getApp().api.cardshop.buy_card,
      data: {
        goods_limit: _goods,
        card_id: card_id,
        total_price: total_price,
        name: card_name
      },
      success: function (e) {
        if(e.code==0){
          console.log(e, '支付参数');
          var order_no = e.data.order_no;
          wx.requestPayment({
            timeStamp: e.data.timeStamp,
            nonceStr: e.data.nonceStr,
            package: e.data.package,
            signType: e.data.signType,
            paySign: e.data.paySign,
            success: function (e) {
              console.log("这里是支付成功"), console.log(e)
              //购买礼品卡
              getApp().request({
                url: getApp().api.cardshop.buy_gift_card,
                data: {
                  goods_limit: _goods,
                  card_id: card_id,
                  total_price: total_price,
                  name: card_name,
                  order_no: order_no
                },
                success: function (e) {
                  console.log(e, '礼品卡');
                  if (e.code == 0) {
                    var order_id = e.order_id;
                    wx.showToast({
                      title: e.msg,
                      icon: "none",
                      duration: 1e3,
                      success: function () {
                        setTimeout(function () {
                          getApp().core.redirectTo({
                            url: "/pages/card_voucher/give/index?buy_id=" + order_id,
                          });
                        }, 2e3);
                      }
                    })
                  } else {
                    wx.showToast({
                      title: e.msg,
                      icon: "none",
                      duration: 1e3
                    })
                  }
                },
                complete: function () { }
              });


            },
            fail: function (e) {
              console.log("这里是支付失败"), console.log(e), wx.showToast({
                title: "支付失败",
                icon: "none",
                duration: 1e3
              })
            }
          });
        }else {
          wx.showToast({
            title: e.msg,
            icon: 'none',            
            duration: 1500,
          })
        }
        

      },
      complete: function () { }
    });   



  },



  //options(Object)
  onLoad: function (options) {
    console.log(options,'卡券id');

    var tath = this;
    var card_id = options.card_id;//卡券id
    var nowRebate = Number(options.rebate);
    
    //查看卡券详情
    getApp().request({
      url: getApp().api.cardshop.get_card,
      data: {
        card_id:card_id,
        
      },
      success: function (t) {
        console.log(t, '卡券详情信息');
        // for (let i = 0; i < t.data.gift_card_goods.length;i++){
          
        //   t.data.gift_card_goods[i].wang = "t.data.gift_card_goods[i].goods_price * nowRebate"

        // }
        console.log(t.data)
 

        tath.setData({
          details:t.data,
          nowRebate: nowRebate
        });
      },
      complete: function () { }
    });

  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  onPageScroll: function () {

  },
  //item(index,pagePath,text)
  onTabItemTap: function (item) {

  }
});